home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / muds / lpmud312.tar / lpmud312 / swap.c < prev    next >
C/C++ Source or Header  |  1991-10-31  |  7KB  |  262 lines

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <stdio.h>
  4. #ifdef __STDC__
  5. #include <memory.h>
  6. #endif
  7.  
  8. #include "lint.h"
  9. #include "interpret.h"
  10. #include "object.h"
  11. #include "config.h"
  12. #include "exec.h"
  13.  
  14. /*
  15.  * Swap out programs from objects.
  16.  */
  17.  
  18. int num_swapped;
  19. int total_bytes_swapped;
  20. char file_name[100];
  21.  
  22. FILE *swap_file;        /* The swap file is opened once */
  23.  
  24. int total_num_prog_blocks, total_prog_block_size;
  25.  
  26. extern int d_flag;
  27.  
  28. /*
  29.  * marion - adjust pointers for swap out and later relocate on swap in
  30.  *   program
  31.  *   line_numbers
  32.  *   functions
  33.  *   strings
  34.  *   variable_names
  35.  *   inherit
  36.  *   argument_types
  37.  *   type_start
  38.  */
  39. int
  40. locate_out (prog) struct program *prog; {
  41.     char *p = 0; /* keep cc happy */
  42.  
  43.     if (!prog) return 0;
  44.     if (d_flag > 1) {
  45.     debug_message ("locate_out: %lX %lX %lX %lX %lX %lX %lX %lX\n",
  46.         prog->program, prog->line_numbers, prog->functions,
  47.         prog->strings, prog->variable_names, prog->inherit,
  48.         prog->argument_types, prog->type_start);
  49.     }
  50.     prog->program    = &p[prog->program - (char *)prog];
  51.     prog->line_numbers    = (unsigned short *)
  52.     &p[(char *)prog->line_numbers - (char *)prog];
  53.     prog->functions    = (struct function *)
  54.     &p[(char *)prog->functions - (char *)prog];
  55.     prog->strings    = (char **)
  56.     &p[(char *)prog->strings - (char *)prog];
  57.     prog->variable_names= (struct variable *)
  58.     &p[(char *)prog->variable_names - (char *)prog];
  59.     prog->inherit    = (struct inherit *)
  60.     &p[(char *)prog->inherit - (char *)prog];
  61.     if (prog->type_start) {
  62.     prog->argument_types = (unsigned short *)
  63.         &p[(char *)prog->argument_types - (char *)prog];
  64.     prog->type_start = (unsigned short *)
  65.         &p[(char *)prog->type_start - (char *)prog];
  66.     }
  67.     return 1;
  68. }
  69.  
  70.  
  71. /*
  72.  * marion - relocate pointers after swap in
  73.  *   program
  74.  *   line_numbers
  75.  *   functions
  76.  *   strings
  77.  *   variable_names
  78.  *   inherit
  79.  *   argument_types
  80.  *   type_start
  81.  */
  82. int
  83. locate_in (prog) struct program *prog; {
  84.     char *p = (char *)prog;
  85.  
  86.     if (!prog) return 0;
  87.     prog->program    = &p[prog->program - (char *)0];
  88.     prog->line_numbers    = (unsigned short *)
  89.     &p[(char *)prog->line_numbers - (char *)0];
  90.     prog->functions    = (struct function *)
  91.     &p[(char *)prog->functions - (char *)0];
  92.     prog->strings    = (char **)
  93.     &p[(char *)prog->strings - (char *)0];
  94.     prog->variable_names= (struct variable *)
  95.     &p[(char *)prog->variable_names - (char *)0];
  96.     prog->inherit    = (struct inherit *)
  97.     &p[(char *)prog->inherit - (char *)0];
  98.     if (prog->type_start) {
  99.     prog->argument_types = (unsigned short *)
  100.         &p[(char *)prog->argument_types - (char *)0];
  101.     prog->type_start     = (unsigned short *)
  102.         &p[(char *)prog->type_start - (char *)0];
  103.     }
  104.     if (d_flag > 1) {
  105.     debug_message ("locate_in: %lX %lX %lX %lX %lX %lX %lX\n",
  106.         prog->program, prog->line_numbers, prog->functions,
  107.         prog->strings, prog->variable_names, prog->inherit,
  108.         prog->argument_types, prog->type_start);
  109.     }
  110.     return 1;
  111. }
  112.  
  113. /*
  114.  * Swap out an object. Only the program is swapped, not the 'struct object'.
  115.  *
  116.  * marion - the swap seems to corrupt the function table
  117.  */
  118. int swap(ob)
  119.     struct object *ob;
  120. {
  121.     if (ob->flags & O_DESTRUCTED)
  122.     return 0;
  123.     if (d_flag > 1) { /* marion */
  124.     debug_message("Swap object %s (ref %d)\n", ob->name, ob->ref);
  125.     }
  126.     if (swap_file == 0) {
  127. #ifndef MSDOS
  128.     char host[50];
  129.     gethostname(host, sizeof host);
  130.     sprintf(file_name, "%s.%s", SWAP_FILE, host);
  131.     swap_file = fopen(file_name, "w+");
  132. #else
  133.     swap_file = fopen(strcpy(file_name,"LPMUD.SWAP"),"w+b");
  134. #endif
  135.     /* Leave this file pointer open ! */
  136.     if (swap_file == 0)
  137.         return 0;
  138.     }
  139.     if (!ob->prog)
  140.     {
  141.     fprintf(stderr, "warning:no program in object %s, don't swap it\n",
  142.         ob->name);
  143.     /* It's no good freeing a NULL pointer */
  144.     return 0;
  145.     }
  146.     if ((ob->flags & O_HEART_BEAT) || (ob->flags & O_CLONE)) {
  147.     if (d_flag > 1) {
  148.         debug_message ("  object not swapped - heart beat or cloned.\n");
  149.     }
  150.     return 0;
  151.     }
  152.     if (ob->prog->ref > 1 || ob->interactive) {
  153.     if (d_flag > 1) {
  154.         debug_message ("  object not swapped - inherited or interactive.\n");
  155.     }
  156.     return 0;
  157.     }
  158.     /*
  159.      * Has this object already been swapped, and read in again ?
  160.      * Then it is very easy to swap it out again.
  161.      */
  162.     if (ob->swap_num >= 0) {
  163.     total_bytes_swapped += ob->prog->total_size;
  164.     free_prog(ob->prog, 0);        /* Do not free the strings */
  165.     ob->prog = 0;
  166.     ob->flags |= O_SWAPPED;
  167.     num_swapped++;
  168.     return 1;
  169.     }
  170.     /*
  171.      * marion - it is more efficient to write one item the size of the
  172.      *   program to the file than many items of size one. besides, it's
  173.      *   much more reasonable, as the fwrite only fails for the whole
  174.      *   block and not for a part of it.
  175.      */
  176.     ob->swap_num = ftell(swap_file);
  177.     locate_out (ob->prog); /* relocate the internal pointers */
  178.     if (fwrite((char *)ob->prog, ob->prog->total_size, 1, swap_file) != 1) {
  179.     debug_message("I/O error in swap.\n");
  180.     ob->swap_num = -1;
  181.     return 0;
  182.     }
  183.     total_bytes_swapped += ob->prog->total_size;
  184.     num_swapped++;
  185.     free_prog(ob->prog, 0);    /* Don't free the shared strings */
  186.     ob->prog = 0;
  187.     ob->flags |= O_SWAPPED;
  188.     return 1;
  189. }
  190.  
  191. void load_ob_from_swap(ob)
  192.     struct object *ob;
  193. {
  194.     extern int errno;
  195.     struct program tmp_prog;
  196.  
  197.     if (ob->swap_num == -1)
  198.     fatal("Loading not swapped object.\n");
  199.     if (fseek(swap_file, ob->swap_num, 0) == -1)
  200.     fatal("Couldn't seek the swap file, errno %d, offset %d.\n",
  201.           errno, ob->swap_num);
  202.     if (d_flag > 1) { /* marion */
  203.     debug_message("Unswap object %s (ref %d)\n", ob->name, ob->ref);
  204.     }
  205.     /*
  206.      * The size of the program is unkown, so read first part to
  207.      * find out.
  208.      *
  209.      * marion - again, the read in a block is more efficient
  210.      */
  211.     if (fread((char *)&tmp_prog, sizeof tmp_prog, 1, swap_file) != 1) {
  212.     fatal("Couldn't read the swap file.\n");
  213.     }
  214.     ob->prog = (struct program *)xalloc(tmp_prog.total_size);
  215.     memcpy((char *)ob->prog, (char *)&tmp_prog, sizeof tmp_prog);
  216.     fread((char *)ob->prog + sizeof tmp_prog,
  217.       tmp_prog.total_size - sizeof tmp_prog, 1, swap_file);
  218.     /*
  219.      * to be relocated:
  220.      *   program
  221.      *   line_numbers
  222.      *   functions
  223.      *   strings
  224.      *   variable_names
  225.      *   inherit
  226.      *     argument_types
  227.      *     type_start
  228.      */
  229.     locate_in (ob->prog); /* relocate the internal pointers */
  230.  
  231.     /* The reference count will already be 1 ! */
  232.     ob->flags &= ~O_SWAPPED;
  233.     total_bytes_swapped -= ob->prog->total_size;
  234.     num_swapped--;
  235.     total_prog_block_size += ob->prog->total_size;
  236.     total_num_prog_blocks += 1;
  237.     if (fseek(swap_file, 0L, 2) == -1) { /* marion - seek back for more swap */
  238.     fatal("Couldn't seek end the swap file, errno %d.\n", errno);
  239.     }
  240. }
  241.  
  242. void remove_swap_file(ob)
  243.     struct object *ob;
  244. {
  245.     /* Haven't implemented this yet :-( */
  246. }
  247.  
  248. /*
  249.  * This one is called at shutdown. Remove the swap file.
  250.  */
  251. void unlink_swap_file() {
  252.     if (swap_file == 0)
  253.     return;
  254. #ifndef MSDOS
  255.     unlink(file_name);
  256.     fclose(swap_file);
  257. #else
  258.     fclose(swap_file);
  259.     unlink(file_name);
  260. #endif
  261. }
  262.